<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="isEdit ? '修改权限' : '新增权限'"
    width="800px"
    :visible.sync="visible"
  >
    <el-form :model="queryParam" ref="form" :rules="rules" label-width="100px">
      <!-- style="width: 440px !important" -->

      <el-form-item label="权限分类" prop="parentId">
        <el-select
          :disabled="parentSelectDis"
          class="mr20"
          style="width: 100%"
          v-model="queryParam.parentId"
          clearable
        >
          <el-option
            v-for="(item, ind) in optionData"
            :key="ind"
            :value="item.permissionId"
            :label="item.permissionName"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="权限名称" prop="permissionName">
        <el-input
          @blur="handleUniquClick"
          type="text"
          clearable
          autocomplete="false"
          placeholder="请输入权限名称"
          v-model="queryParam.permissionName"
        />
        <!-- <el-button @click="handleUniquClick">唯一性检测</el-button> -->
      </el-form-item>

      <el-form-item label="权限显示名" prop="permissionShowName">
        <el-input
          type="text"
          autocomplete="false"
          placeholder="请输入权限显示名称"
          v-model="queryParam.permissionShowName"
        />
      </el-form-item>

      <el-form-item label="排序" prop="permissionOrder">
        <el-input-number :min="0" :max="100" v-model.number="queryParam.permissionOrder" />
      </el-form-item>

      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParam.state" clearable>
          <el-option :value="0" label="启用" />
          <el-option :value="1" label="禁用" />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click.stop="handleOk">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updatePermissionInfo, createPermissionInfo, queryOnlyPermissionName, queryTopPermissionList } from '@/api/role'
import { openNotification } from '@/utils/util'

export default {
  data() {
    const validPermissionName = (rule, value, callback) => {
      if (!value) {
        callback('请输入权限名称')
      } else {
        callback()
      }
    }
    return {
      parentSelectDis: false,
      visible: false,
      isEdit: false,
      confirmLoading: false,
      queryParam: {
        parentId: null,
        permissionId: '',
        permissionName: '',
        permissionShowName: '',
        permissionOrder: '',
        state: ''
      },
      rules: {
        parentId: [{ required: true, message: '请选择权限分类', trigger: 'blur' }],
        permissionName: [{ required: true, validator: validPermissionName, trigger: 'blur' }],
        permissionShowName: [{ required: true, message: '请输入权限显示名称', trigger: 'blur' }],
        permissionOrder: [
          { required: true, message: '请输入权限排序值', trigger: 'blur' },
          { type: 'number', message: '权限排序值必须为数字' }
        ]
      },
      optionData: [],
      validateStatus: '',
      errMsg: ''
    }
  },
  mounted() {
    queryTopPermissionList().then(res => {
      console.log('res', res)

      this.optionData = res.data
    })
  },
  methods: {
    checkChange(e) {
      if (e) {
        this.parentSelectDis = true
        this.queryParam.parentId = ''
      } else {
        this.parentSelectDis = false
      }
    },
    edit(bool, record) {
      this.isEdit = bool
      console.log('record', record)
      this.queryParam = Object.assign({}, record)
      if (!bool) {
        // 新增
        this.$set(this.queryParam, 'permissionOrder', 0)
        this.$set(this.queryParam, 'state', 0)
      }
      this.visible = true
    },
    handleOk(e) {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.isEdit) {
            const res = await updatePermissionInfo(this.queryParam)
          } else {
            const res = await createPermissionInfo(this.queryParam)
          }
          const mes = this.isEdit ? '修改操作成功' : '新增操作成功'
          openNotification('success', mes)
          this.visible = false
          this.$emit('refresh')
        }
      })
    },
    async handleUniquClick() {
      if (this.queryParam.permissionName) {
        const param = { permissionName: this.queryParam.permissionName }
        const res = await queryOnlyPermissionName(param)
        if (res !== undefined) {
          this.errMsg = ''
          this.validateStatus = 'success'
        } else {
          this.errMsg = '权限名称重复'
          this.validateStatus = 'error'
        }
      }
    }
  }
}
</script>

<style scoped>
.short-input {
  width: 380px;
  margin-right: 19px;
}
</style>
